<?php


namespace App\Modules\Admin\Services\Seller;


use App\Modules\Admin\Entities\Seller\SellerModel;
use App\Modules\Admin\Services\BaseService;
use Illuminate\Support\Facades\DB;

class SellerServices extends BaseService
{

    public function __construct(SellerModel $seller)
    {
        $this->model = $seller;
    }

    public function lists(array $params) : array
    {
        $params['where_callback'] = function($query) use ($params){
            $request = request();
            // 按照名称进行搜索
            /*if (!empty($search = $request->input('search', ''))){
                $search = trim($search);
                $query->where(function($query)use ($search){
                    $query->where('seller_name', 'LIKE', '%' . $search . '%');
                });
            }
            // 置顶
            $set_top = $request->input('set_top', -1);
            if ($set_top > -1){
                $query->where('set_top', '=', $set_top);
            }
            // 推荐
            $is_recommend = $request->input('is_recommend', -1);
            if ($is_recommend > -1){
                $query->where('is_recommend', '=', $is_recommend);
            }
            // 是否公开
            $is_public = $request->input('is_public', -1);
            if ($is_public > -1){
                $query->where('is_public', '=', $is_public);
            }*/
        };
        return parent::lists($params); // TODO: Change the autogenerated stub
    }

    public function create(array $params)
    {
        DB::beginTransaction();
        try{
             $data = parent::create($params); // TODO: Change the autogenerated stub

            $this->sellerAdminCreate($params);
            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            return false;
        }
    }

    public function update(array $params)
    {
        DB::beginTransaction();
        try{
            exit(json_encode($this->detail));
            parent::update($params); // TODO: Change the autogenerated stub

            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            return false;
        }
    }

    private function sellerAdminCreate($params){

//        exit(json_encode($this->model->getKeyName()));
//        exit(json_encode($this->detail));
        $sellerAdmin = SellerAdminServices::getInstance();
       $id= $sellerAdmin->create([
            'account' => $this->detail->account,
            'password' => $this->detail->password,
            'seller_id' =>  $this->detail->{$this->model->getKeyName()}
        ]);
        exit(json_encode($id));
    }

    public function getSelectLists($request){
        return $this->model->getSelectLists();
    }
}